home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / snap_1_4 / part01 / source / openclose.c < prev    next >
Text File  |  1990-02-11  |  8KB  |  264 lines

  1. /* Auto: make
  2. */
  3.  
  4. IMPORT LONGBITS startsignal, insertsignal, cancelsignal, donesignal;
  5. IMPORT LONGBITS movesignal, clicksignal, timersignal, initsignal, cwsignal;
  6. IMPORT ULONG startsignum, insertsignum, cancelsignum, donesignum;
  7. IMPORT ULONG movesignum, clicksignum, timersignum, initsignum, cwsignum;
  8.  
  9. /* program */
  10. IMPORT struct SnapRsrc *SnapRsrc;
  11. IMPORT struct Task *MyTask;
  12.  
  13. /* Snap state machine */
  14. IMPORT WORD action;
  15. IMPORT WORD state;
  16.  
  17. /* clipboard */
  18. IMPORT struct IOClipReq *ClipReq;
  19. IMPORT struct MsgPort *ClipPort;
  20.  
  21. /* timer device */
  22. IMPORT struct MsgPort *TimerPort;
  23. IMPORT struct timerequest MyTR;
  24.  
  25. /* input device */
  26. IMPORT struct MsgPort *inputDevPort;
  27. IMPORT struct Interrupt handlerStuff;
  28. IMPORT struct IOStdReq *inputRequestBlock;
  29.  
  30. IMPORT UBYTE *CharData;
  31.  
  32. /* console */
  33. IMPORT struct MsgPort *ConPort;
  34. IMPORT struct IOStdReq *ConIOR;
  35. IMPORT struct KeyMap keymap;
  36.  
  37. /* windows */
  38. IMPORT struct MsgPort *Sharedport;
  39. IMPORT SHORT Sharedrefs;
  40. IMPORT struct MinList CachedWindows;
  41.  
  42. /* libraries */
  43. IMPORT struct IntuitionBase *IntuitionBase;
  44. IMPORT struct GfxBase       *GfxBase;
  45. IMPORT struct LayersBase    *LayersBase;
  46. IMPORT struct ArpBase       *ArpBase;
  47.  
  48. /* graphics */
  49. IMPORT struct RastPort TempRp;
  50. IMPORT struct BitMap TempBM;
  51. IMPORT UBYTE *TempRaster;
  52.  
  53. IMPORT struct Image DiskImage;
  54. UWORD ImData[24] = {
  55.     /* BitPlane 0 */
  56.     0xFFFF,
  57.     0xF00F,
  58.     0xF7EF,
  59.     0xF42F,
  60.     0xF7EF,
  61.     0xF00F,
  62.     0xF3CF,
  63.     0xF2CF,
  64.     0xF2CF,
  65.     0xFFFF,
  66.     0xFFFF,
  67.     0xFFFF,
  68.     0xFFFF,
  69.     0xFFFF,
  70.     0xFFFF,
  71.     0xFFFF,
  72.     0xFFFF,
  73.     0xFFFF,
  74.     0xFFFF,
  75.     0xFFFF,
  76.     0xFFFF,
  77.     0xFFFF,
  78.     0xFFFF,
  79.     0xFFFF
  80. };
  81.  
  82. VOID CloseStuff()
  83. {
  84.     SafeRestore();
  85.     {
  86.         struct CacheWindow *cw;
  87.         while (cw = (struct CacheWindow *)
  88.           RemHead((struct List *)&CachedWindows)) {
  89.             FreeMem(cw, (LONG)sizeof(struct CacheWindow));
  90.         }
  91.     }
  92.     if (DiskImage.ImageData) FreeMem(DiskImage.ImageData, 32L);
  93.     if (TempRaster)         FreeRaster(TempRaster, 16L, 16L);
  94.     if (CharData)           FreeMem(CharData, 256L*32);
  95.     if (inputRequestBlock) {
  96.         if (inputRequestBlock->io_Device) {
  97.             inputRequestBlock->io_Command = IND_REMHANDLER;  /* Remove handler */
  98.             inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  99.             DoIO(inputRequestBlock);
  100.             CloseDevice(inputRequestBlock);
  101.         }
  102.         DeleteExtIO(inputRequestBlock);
  103.     }
  104.     if (inputDevPort)       DeletePort(inputDevPort);
  105.     if (TimerPort) {
  106.         DeletePort(TimerPort);
  107.         CloseDevice((struct IOStdReq *)&MyTR);
  108.     }
  109.     if (ConIOR) {
  110.         CloseDevice(ConIOR);
  111.         DeleteExtIO(ConIOR);
  112.     }
  113.     if (ConPort)            DeletePort(ConPort);
  114.     if (ClipReq) {
  115.         if (ClipReq->io_Device) {
  116.             CloseDevice(ClipReq);
  117.         }
  118.         DeleteExtIO(ClipReq);
  119.     }
  120.     if (ClipPort)           DeletePort(ClipPort);
  121.     if (startsignum != -1)  FreeSignal(startsignum);
  122.     if (donesignum != -1)   FreeSignal(donesignum);
  123.     if (cancelsignum != -1) FreeSignal(cancelsignum);
  124.     if (movesignum != -1)   FreeSignal(movesignum);
  125.     if (insertsignum != -1) FreeSignal(insertsignum);
  126.     if (clicksignum != -1)  FreeSignal(clicksignum);
  127.     if (timersignum != -1)  FreeSignal(timersignum);
  128.     if (initsignum != -1)   FreeSignal(initsignum);
  129.     if (cwsignum != -1)     FreeSignal(cwsignum);
  130.     if (SnapRsrc) {
  131.         RemResource(SnapRsrc);
  132.         Kill(SnapRsrc);
  133.     }
  134.     if (ArpBase)            CloseLibrary((struct Library *)ArpBase);
  135.     if (IntuitionBase)      CloseLibrary((struct Library *)IntuitionBase);
  136.     if (GfxBase)            CloseLibrary((struct Library *)GfxBase);
  137.     if (LayersBase)         CloseLibrary((struct Library *)LayersBase);
  138. }
  139.  
  140. WORD OpenStuff()
  141. {
  142.     action = noaction;
  143.     state  = waiting;
  144.     inputRequestBlock = NULL;
  145.  
  146.     Sharedrefs = 0;
  147.     Sharedport = NULL;
  148.     NewList((struct NewList *)&CachedWindows);
  149.  
  150.     /* Set up everything we need. */
  151.  
  152.     /* libraries */
  153.  
  154.     if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L)))
  155.         return 0;
  156.     if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0L)))
  157.         return 0;
  158.     if (!(LayersBase = (struct LayersBase *)OpenLibrary("layers.library", 0L)))
  159.         return 0;
  160.     ArpBase = (struct ArpBase *)OpenLibrary(ArpName, ArpVersion);
  161.  
  162.     /* signals */
  163.  
  164.     if ((startsignum = AllocSignal(-1L)) == -1L)
  165.         return 0;
  166.     if ((insertsignum = AllocSignal(-1L)) == -1L)
  167.         return 0;
  168.     if ((cancelsignum = AllocSignal(-1L)) == -1L)
  169.         return 0;
  170.     if ((donesignum = AllocSignal(-1L)) == -1L)
  171.         return 0;
  172.     if ((movesignum = AllocSignal(-1L)) == -1L)
  173.         return 0;
  174.     if ((clicksignum = AllocSignal(-1L)) == -1L)
  175.         return 0;
  176.     if ((timersignum = AllocSignal(-1L)) == -1L)
  177.         return 0;
  178.     if ((initsignum = AllocSignal(-1L)) == -1L)
  179.         return 0;
  180.     if ((cwsignum = AllocSignal(-1L)) == -1L)
  181.         return 0;
  182.     MyTask = FindTask(NULL);                  /* Find myself to Signal me.  */
  183.     startsignal  = 1L << startsignum;         /* No good to use bit numbers */
  184.     insertsignal = 1L << insertsignum;
  185.     cancelsignal = 1L << cancelsignum;
  186.     donesignal   = 1L << donesignum;
  187.     movesignal   = 1L << movesignum;
  188.     clicksignal  = 1L << clicksignum;
  189.     timersignal  = 1L << timersignum;
  190.     initsignal   = 1L << initsignum;
  191.     cwsignal     = 1L << cwsignum;
  192.  
  193.     /* clipboard device */
  194.  
  195.     if (!(ClipPort = CreatePort(0L, 0L)))
  196.         return 0;
  197.     if (!(ClipReq = (struct IOClipReq *)CreateExtIO(ClipPort, (LONG)sizeof(*ClipReq))))
  198.         return 0;
  199.     if (OpenDevice("clipboard.device", 0L, ClipReq, 0L))
  200.         return 0;
  201.     ClipReq->io_ClipID = 0L;
  202.  
  203.     /* console device */
  204.  
  205.     if (!(ConPort = CreatePort(0L, 0L)))
  206.         return 0;
  207.     if (!(ConIOR = (struct IOStdReq *)CreateExtIO(ConPort, (LONG)sizeof(struct IOStdReq))))
  208.         return 0;
  209.     if (OpenDevice("console.device", -1L, ConIOR, 0L))
  210.         return 0;
  211.  
  212.     /* timer device */
  213.     if (!(TimerPort = CreatePort(NULL, 0L)))
  214.         return 0;
  215.     if (OpenDevice(TIMERNAME, UNIT_MICROHZ, (struct IOStdReq *)&MyTR, 0L))
  216.         return 0;
  217.     MyTR.tr_node.io_Message.mn_ReplyPort = TimerPort;
  218.     MyTR.tr_node.io_Command = TR_ADDREQUEST;
  219.  
  220.     /* input devive */
  221.  
  222.     if (!(inputDevPort = CreatePort(0L, 0L)))
  223.         return 0;
  224.     if (!(inputRequestBlock = (struct IOStdReq *)CreateExtIO(inputDevPort, (LONG)sizeof(struct IOStdReq))))
  225.         return 0;
  226.     if (OpenDevice("input.device", 0L, inputRequestBlock, 0L))
  227.         return 0;
  228.  
  229.     /* input handler */
  230.  
  231.     handlerStuff.is_Data = (APTR)0x534E4150; /* Set up for installation of */
  232.     handlerStuff.is_Code = myhandler;        /* myhandler.                 */
  233.     handlerStuff.is_Node.ln_Pri = SnapRsrc->Priority;
  234.                                              /* Ahead of intuition, please */
  235.     handlerStuff.is_Node.ln_Name = "Snap Input Handler";
  236.  
  237.     inputRequestBlock->io_Command = IND_ADDHANDLER;
  238.     inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  239.  
  240.     DoIO(inputRequestBlock);  /* Add me. */
  241.  
  242.  
  243.     /* Aligned font bitmap to use when matching */
  244.  
  245.     if (!(CharData = AllocRaster(16L, 256L*16))) {
  246.         return 0;
  247.     }
  248.  
  249.     /* temporary raster */
  250.  
  251.     if (!(TempRaster = AllocRaster(16L, 16L)))
  252.         return 0;
  253.     InitRastPort(&TempRp);                   /* Init RastPort used for */
  254.     InitBitMap(&TempBM, 1L, 16L, 16L);       /* Locating position of   */
  255.     TempBM.Planes[0] = TempRaster;           /* first character.       */
  256.     TempRp.BitMap = &TempBM;
  257.  
  258.     if (!(DiskImage.ImageData = AllocMem(48L, MEMF_CHIP))) {
  259.         return 0;
  260.     }
  261.     CopyMem((char *)&ImData[0], (char *)DiskImage.ImageData, 48L);
  262.     return 1;
  263. }
  264.